
var waitHtml='<img src="../../document/assets/global/img/loading-spinner-grey.gif" alt="" class="loading"><span> &nbsp;&nbsp;Loading... </span>';

var newData1;
var newData2;
var newData3;
var pageInit=500;


/* 招标数据详情按钮:触发modal */
function snapshot_modal(districtNewsId, key, province_id){
    $.post(
        "/home/ajax_detailsPage",
        {
            districtNewsId: districtNewsId,
            keyList: key,
            location: province_id
        },
        function(data){
            var news=data.news;
            var title;
            if(news.title.length>40){
                title=news.title.substring(0,40)+"...";
            }else{
                title=news.title;
            }
            $("#newsTitle_xq").text(title);
            $("#tenderId").text(news.tenderId);
            $("#qualification").text(news.qualification);

            var itemAmount=news.item_amount;
            $("#itemAmount").text(itemAmount==0?"":(itemAmount/10000).toFixed(4));

            $("#m_LTime").text(news.lTime);
            $("#m_startTime").text(news.startTime);
            $("#m_endTime").text(news.endTime);

            $("#subscriber").text(news.subscriber);
            var contact=news.subscriber_contact;
            if(contact=="无"){
                contact="";
            }
            $("#subscriber_contact").text(contact);
            $("#subscriber_phone").text(news.subscriber_phone);

            $("#agent").text(news.agent);
            $("#agent_contact").text(news.agent_contact);
            $("#agent_phone").text(news.agent_phone);

            $("#snapShot").html('<a href="http://zb.zbmao.cn/news/snapShot?districtNewsId='+districtNewsId+'&keyList='+key+'&location='+data.location+'" target="_blank">点击查看快照</a>');

            $("#location").text(data.locationStr);
            $("#modal_xq").modal('show');
        }
    );
}


var tableHead = '<table class="table table-striped table-bordered table-hover table-checkable dataTable " id="tables" aria-describedby="datatable_ajax_info" role="grid">' +
    '<thead>' +
    '<tr role="row" class="heading">' +
    '<th width="5%" tabindex="0" aria-controls="datatable_ajax" rowspan="1" colspan="1">' +
    'id' +
    '</th>' +
    '<th width="200"  tabindex="0" aria-controls="datatable_ajax" rowspan="1" colspan="1">' +
    '标题' +
    '</th>' +
    '<th width="10%"  tabindex="0" aria-controls="datatable_ajax" rowspan="1" colspan="1">' +
    '投标截止日期' +
    '</th>' +
    '<th width="10%"  tabindex="0" aria-controls="datatable_ajax" rowspan="1" colspan="1">' +
    '最高限价(万元)' +
    '</th>' +
    '<th width="10%"  tabindex="0" aria-controls="datatable_ajax" rowspan="1" colspan="1">' +
    '资质' +
    '</th>' +
    '<th width="10%"  tabindex="0" aria-controls="datatable_ajax" rowspan="1" colspan="1">' +
    '详情' +
    '</th>' +
    '</tr>' +
    '</thead>' +
    '<tbody></tbody></table>';




/**
 * 加载数据时不能点击其他tab
 */
function unOnclick(){
    $("#newsDiv1").addClass("disabled").children().attr("href", "javascript:;");
    $("#newsDiv1").addClass("disabled").children().attr("onclick", "");
    $("#newsDiv2").addClass("disabled").children().attr("href", "javascript:;");
    $("#newsDiv2").addClass("disabled").children().attr("onclick", "");
    $("#newsDiv3").addClass("disabled").children().attr("href", "javascript:;");
    $("#newsDiv3").addClass("disabled").children().attr("onclick", "");
}

function tableData(num,nowNum) {

    var provice;
    var newsAllSearchKey;
    var newsUnSearchKey;
    var newsSearchKey;
    if (num ==1  ||nowNum==1) {
        provice = newData1.province1;
        newsAllSearchKey = newData1.newsAllSearchKey1;
        newsUnSearchKey = newData1.newsUnSearchKey1;
        newsSearchKey = newData1.newsSearchKey1;
    } else if (num == 2) {
        provice = newData2.province2;
        newsAllSearchKey = newData2.newsAllSearchKey2;
        newsUnSearchKey = newData2.newsUnSearchKey2;
        newsSearchKey = newData2.newsSearchKey2;
    } else if (num == 3) {
        provice = newData3.province3;
        newsAllSearchKey = newData3.newsAllSearchKey3;
        newsUnSearchKey = newData3.newsUnSearchKey3;
        newsSearchKey = newData3.newsSearchKey3;
    }

    if(nowNum==1 && num!=1){

    }else{
        $(".table-container").html(tableHead);
    }

    var datas;
    $("#newsDiv" + typsNews).removeClass("active");
    $("#tab" + typsNews).removeClass("active");
    if (num == 1 ||nowNum==1) {
        $("#newsDiv1").addClass("active");
        $("#tab1").addClass("active");
        typsNews = 1;
        datas = newData1.news1;
    } else if (num == 2) {
        $("#newsDiv2").addClass("active");
        $("#tab2").addClass("active");
        typsNews = 2;
        datas = newData2.news2;
    } else if (num == 3) {
        $("#newsDiv3").addClass("active");
        $("#tab3").addClass("active");
        typsNews = 3;
        datas = newData3.news3;
    }
    if(datas==null){
        return;
    }

    $(".widget-thumb-body-stat").text(datas.length)
    $(".dy").text(provice);
    $(".ask").text(newsAllSearchKey);
    $(".usk").text("剔除关键词：" + newsUnSearchKey);
    $(".sk").text("专属关键词：" + newsSearchKey);

    $('#tables').DataTable({
        data: datas,
        bAutoWidth: true,//自动宽度
        iDisplayLength: 50,  //确定每页展示数量
        bPaginate: true, //翻页功能
        bLengthChange: false,  //是否需要选择每页展示数据
        bFilter: false,  //是否开启过滤功能
        bSort:false,  //排序功能
        bDestroy:true, //屏蔽错误提示
        bSort:false,  //排序功能
        oLanguage: {  //表格基本信息翻译
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "抱歉， 没有找到数据",
            "sInfo": "",
            "sInfoEmpty": "没有数据",
            "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上一页",
                "sNext": "下一页",
                "sLast": "尾页"
            }
        }
    });


    if (datas.length >= pageInit || datas.length%500==0) {
        $("#tables_wrapper").append("<div><ul class='pagination' style='position: absolute;left:65%;bottom: -5px'><li><a type='button' onclick='getNews(\"" + num + "\");'>点击加载更多</a></li></ul></div>");
    }
}

var subscriber="";
var title_key="";
//拼接该条公告进本信息数据拼接展示
function detailsPage(district_news_id, key, province_id) {
    $("#news").html(waitHtml);
    $("#news1").html(waitHtml);
    $("#news3").html(waitHtml);
//        查询出招标单位为其他功能服务
    $.ajax({
        async: false,
        type: "POST",
        url: "../tool/findSubscriberByDistrict",
        dataType: 'json',
        data: {
            districtNewsId: district_news_id,
            province_id: province_id
        },
        success: function (data) {
            subscriber = data.subscriber;
            title_key=data.title_key;
        }
    });
    $("#modalClose").modal('show');

//        中标信息查询
    newsToSubscriber();
    // 招标联系人查询
    findContact();


}

//招标单位查中标供应商
function newsToSubscriber(){

    if(subscriber==null ||subscriber == ""){
        $("#news1").html("<div style='height:100px;padding-top:50px;text-align: center'>" +
        "该招标信息未找到招标单位，无法检索到对应中标信息！</div>");
        newsToWinBidCompany();
        return;
    }
    $.post(
        "../tool/findSuccessNewsAjax",
        {
            subscriber:subscriber,
            title_key:title_key
        },
        function(data){
            var newsSize=data.newsSize;
            if(newsSize>0){
                var html=' <table  style="text-align: center;width: 750px;" id="tableSuccessNews" aria-describedby="datatable_ajax_info" role="grid">';
                html+='<thead><tr>';
                html+='<th style="width:50px;"> ID </th>';
                html+='<th> 中标标题 </th>';
                html+='<th style="width:100px;"> 中标时间</th>';
                html+='</tr> </thead>';
                html+='<tbody  id="wait"></tbody>';
                html+='</table>';
                $("#news1").html(html);
                var news=data.listNews;
                $('#tableSuccessNews').DataTable({
                    data: eval(news),
                    iDisplayLength: 4,  //确定每页展示数量
                    bLengthChange: false,  //是否需要选择每页展示数据
                    bFilter: false,  //是否开启过滤功能
                    bSort:false,  //排序功能
                    bDestroy: true, //屏蔽错误提示
                    oLanguage: {  //表格基本信息翻译

                        "sLengthMenu": "每页显示 _MENU_ 条记录",
                        "sZeroRecords": "抱歉， 没有找到数据",
                        "sInfo": "",
                        "sInfoEmpty": "没有数据",
                        "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                        "oPaginate": {
                            "sFirst": "首页",
                            "sPrevious": "上一页",
                            "sNext": "下一页",
                            "sLast": "尾页"
                        }
                    }
                });

                $("#tableNews_wrapper").addClass("table_margin");
            }else{
                $("#news1").html("<div style='height:100px;padding-top:50px;text-align: center'>" +
                "未查询到该招标单位对应的中标信息！</div>");
            }
            newsToWinBidCompany();
        } );
}


//  搜索招标单位相关中标信息
function newsToWinBidCompany(){
    if(subscriber==null ||subscriber == ""){
        $("#news3").html("<div style='height:100px;padding-top:50px;text-align: center'>" +
        "该招标信息为空或未找到中标信息，无法检索到对应中标供应商！</div>");
        return;
    }

    $.post(
        "../tool/findSuccessfulCompanyAjax",
        {
            subscriber:subscriber,
            title_key:title_key
        },
        function(data){
            var newsSize=data.newsSize;

            if(newsSize>0){
                var html=' <table  style="text-align: center;width: 750px;" id="tableNewsWinBidCompany" aria-describedby="datatable_ajax_info" role="grid">';
                html+='<thead><tr>';
                html+='<th style="width:30px;"> ID </th>';
                html+='<th  > 中标公司 </th>';
                html+='<th style="width:30px;"> 中标次数 </th>';
                html+='</tr> </thead>';
                html+='<tbody  id="wait"></tbody>';
                html+='</table>';
                $("#news3").html(html);
                var listNews=data.listNews;

                $('#tableNewsWinBidCompany').DataTable({
                    data: eval(listNews),
                    iDisplayLength: 4,  //确定每页展示数量
                    bLengthChange: false,  //是否需要选择每页展示数据
                    bFilter: false,  //是否开启过滤功能
                    bSort:false,  //排序功能
                    bDestroy: true, //屏蔽错误提示
                    oLanguage: {  //表格基本信息翻译
                        "sLengthMenu": "每页显示 _MENU_ 条记录",
                        "sZeroRecords": "抱歉， 没有找到数据",
                        "sInfo": "",
                        "sInfoEmpty": "没有数据",
                        "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                        "oPaginate": {
                            "sFirst": "首页",
                            "sPrevious": "上一页",
                            "sNext": "下一页",
                            "sLast": "尾页"
                        }
                    }
                });
                $("#tableNewsWinBidCompany_wrapper").addClass("table_margin");
            }else{
                $("#news3").html("<div style='height:100px;padding-top:50px;text-align: center'>" +
                "未查询到该招标单位对应的中标供应商！</div>");
                return;
            }
        }
    );
}

function findContact(){

    $.post(
        "../tool/findAllSubscriberContact",
        {
            subscriber:subscriber,
            titleKey:title_key
        },
        function (data) {
            var html=' <table  style="text-align: center;width: 750px;"  id="tableNewsContact" aria-describedby="datatable_ajax_info" role="grid">';
            html+='<thead><tr>';
            html+='<th style="width:50px;"> ID </th>';
            html+='<th style="width:600px;"> 招标单位联系人 </th>';
            html+='<th style="width:600px;"> 招标单位联系联系电话 </th>'
            html+='</tr> </thead>';
            html+='<tbody  id="wait"></tbody>';
            html+='</table>';
            $("#news").html(html);
            var newsSize=data.newsSize;
            if(newsSize>0) {
                var news=data.listNews;
                $('#tableNewsContact').DataTable({
                    data: eval(news),
                    iDisplayLength: 4,  //确定每页展示数量
                    bLengthChange: false,  //是否需要选择每页展示数据
                    bFilter: false,  //是否开启过滤功能
                    bSort: false,  //排序功能
                    bDestroy: true, //屏蔽错误提示
                    oLanguage: {  //表格基本信息翻译
                        "sLengthMenu": "每页显示 _MENU_ 条记录",
                        "sZeroRecords": "抱歉， 没有找到数据",
                        "sInfo": "",
                        "sInfoEmpty": "没有数据",
                        "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                        "oPaginate": {
                            "sFirst": "首页",
                            "sPrevious": "上一页",
                            "sNext": "下一页",
                            "sLast": "尾页"
                        }
                    }
                });

                $("#tableNewsContact_wrapper").addClass("table_margin");
            }else{
                $("#news").html("<div style='height:100px;padding-top:50px;text-align: center'>" +
                "未查询到该招标单位对应的联系方式！</div>");
            }
        });

}